/**
 * 项目名称:
 *
 * @Author: 夏国倩
 * @Date: 2023/10/12 15:39
 * @Copyright
 */
package multithreading;
/*
面试题
 */
public class ThreadTest15 {
    public static void main(String[] args) throws InterruptedException {
        MyClass mc=new MyClass();
        Processor15 p=new Processor15(mc);

        Thread t1=new Thread(p);
        t1.setName("t1");
        Thread t2=new Thread(p);
        t2.setName("t2");

        //启动线程
        t1.start();

        //延迟(保证t1先启动，并执行run)
        Thread.sleep(1000);
        t2.start();
    }
}
class Processor15 implements Runnable{
    MyClass mc;
    public  Processor15(MyClass mc){
        this.mc=mc;
    }
    @Override
    public void run() {
        if (Thread.currentThread().getName().equals("t1")){
            mc.m1();
        }
        if (Thread.currentThread().getName().equals("t2")){
            mc.m2();
        }
    }
}
class  MyClass{
    public synchronized void m1(){
        try {
            Thread.sleep(10000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        System.out.println("m1.......");
    }
    //m2方法的执行不需要等m1的结束，因为m2方法上，没有synchronized
    //m2方法会等m1方法执行结束，t1,t2共享一个mc,并且m1和m2方法上都有synchronized
    public void m2(){
        System.out.println("m2......");
    }
}